package com.xyz.frame.api.controller;

import cn.hutool.core.util.IdUtil;
import com.xyz.frame.bean.model.Weight;
import com.xyz.frame.bean.param.WeightListParam;
import com.xyz.frame.bean.param.WeightParam;
import com.xyz.frame.bean.vo.WeightListVO;
import com.xyz.frame.common.response.ServerResponseEntity;
import com.xyz.frame.security.api.util.SecurityUtils;
import com.xyz.frame.service.WeightService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
//@RequestMapping("/wx/diet")
@RequestMapping("/p/wx/diet")
@Tag(name = "体重接口")
@AllArgsConstructor
@Slf4j
public class WeightController {
    @Autowired
    private WeightService weightService;

    @GetMapping("/get")
    @Operation(summary = "获取体重", description = "基于日期获取用户的体重")
    public ServerResponseEntity<Double> getWeight(@RequestParam(value = "selectVal") String selectVal) throws ParseException {
        String userId = SecurityUtils.getUser().getUserId();
//        String userId = "b075dde4f953469bb952cfb2dc67665e";
        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 将字符串解析为日期对象
        Date date = dateFormat.parse(selectVal);
        Weight oldWeight = weightService.getWeight(userId, date);
        Double weight = null;
        if (oldWeight != null) {
            weight = oldWeight.getWeight();
        }
        return ServerResponseEntity.success(weight);
    }

    @PostMapping("/list")
    @Operation(summary = "获取体重列表", description = "基于日期获取用户体重的列表")
    public ServerResponseEntity<List<WeightListVO>> getWeightList(@RequestBody WeightListParam weightListParam) throws ParseException {
        String userId = SecurityUtils.getUser().getUserId();
        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 将字符串解析为日期对象
        Date startDate = dateFormat.parse(weightListParam.getStartDate());
        Date endDate = dateFormat.parse(weightListParam.getEndDate());
        List<WeightListVO> weightList = weightService.getWeightList(startDate, endDate, userId);
        return ServerResponseEntity.success(weightList);
    }

    @PostMapping("/update")
    @Operation(summary = "修改体重", description = "添加或修改用户的体重数据")
    public ServerResponseEntity<Void> updateWeight(@RequestBody WeightParam weightParam) throws ParseException {
        log.info("weightParam:{}",weightParam);
        String userId = SecurityUtils.getUser().getUserId();
//        String userId = "b075dde4f953469bb952cfb2dc67665e";
        // 定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 将字符串解析为日期对象
        Date date = dateFormat.parse(weightParam.getSelectVal());
        Date now = new Date();
        Weight weight = new Weight();
        weight.setWeight(weightParam.getWeight());
        weight.setUserId(userId);
        weight.setSubmitDate(date);
        weight.setModifyTime(now);
        Weight oldWeight = weightService.getWeight(userId, weight.getSubmitDate());
        if (oldWeight == null) {
            // 新增
            String weightId = IdUtil.simpleUUID();
            weight.setWeightId(weightId);
            weightService.add(weight);
        } else {
            // 修改
            weight.setWeightId(oldWeight.getWeightId());
            weightService.update(weight);
        }
        return ServerResponseEntity.success();
    }
}
